<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:db="http://docbook.org/ns/docbook"
  xmlns:pbs="http://schema.pbs.gov.au" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:skos="http://www.w3.org/2004/02/skos/core#">

  <db:article>
    <db:info>
      <db:title>Extract SN20DR File</db:title>

      <db:copyright>
        <db:year>2012</db:year>
        <db:year>2011</db:year>
        <db:holder>Commonwealth of Australia</db:holder>
      </db:copyright>

      <db:legalnotice>
        <db:para>This software is copyrighted by the Commonwealth of Australia ("The Commonwealth").
          The following terms apply to all files associated with the software unless explicitly
          disclaimed in individual files.</db:para>
        <db:para>Permission is hereby granted to use, copy, modify, distribute, and license this
          software and its documentation for any purpose, provided that existing copyright notices
          are retained in all copies and that this notice is included verbatim in any distributions.
          No written agreement, license, or royalty fee is required for any of the authorized uses.
          Modifications to this software may be copyrighted by their authors and need not follow the
          licensing terms described here, provided that the new terms are clearly indicated on the
          first page of each file where they apply.</db:para>
        <db:para>IN NO EVENT SHALL THE COMMONWEALTH OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
          DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF
          THIS SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE
          BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</db:para>
        <db:para>THE COMMONWEALTH SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED
          TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
          NON-INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, AND THE COMMONWEALTH HAS
          NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
          MODIFICATIONS.</db:para>
      </db:legalnotice>

      <db:author>
        <db:personname>Steve Ball</db:personname>
        <db:email>Steve.Ball@health.gov.au</db:email>
        <db:affiliation>
          <db:orgname>Department of Health and Ageing</db:orgname>
          <db:orgdiv>PharmBiz</db:orgdiv>
        </db:affiliation>
      </db:author>

      <db:revhistory>
        <db:revision>
          <db:revnumber>2.3.2</db:revnumber>
          <db:date>2012-11-07</db:date>
          <db:authorinitials>DMM</db:authorinitials>
          <db:revremark>Added documentation on the output format.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.3.1</db:revnumber>
          <db:date>2012-08-21</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Performance optimisation</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.3</db:revnumber>
          <db:date>2012-08-15</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Exclude changes-list</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.2</db:revnumber>
          <db:date>2012-05-15</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Exclude extemporaneous preparation prescribing rules</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.1</db:revnumber>
          <db:date>2011-11-24</db:date>
          <db:authorinitials>BD</db:authorinitials>
          <db:revremark>Only one row per prescribing-rule code</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.0</db:revnumber>
          <db:date>2011-11-14</db:date>
          <db:authorinitials>DMM</db:authorinitials>
          <db:revremark>Modified keys to address performance issues</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>1.0</db:revnumber>
          <db:date>2011-06-24</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Initial version.</db:revremark>
        </db:revision>
      </db:revhistory>
    </db:info>

    <db:para>This XSL stylesheet produces the SN20DR extract file (<db:filename>sn20dr yyyymmdd.txt</db:filename>) from a PBS XML document.</db:para>
    <db:para>The source document must be a PBS XML document.</db:para>
    <db:para>The result document is a text file.</db:para>
    <db:para>The default field delimiter is the tab character.</db:para>
    <db:table>
      <db:tgroup cols="4" align="left" colsep="1" rowsep="1">
        <db:colspec colnum="3" align="right"/>
        <db:thead>
          <db:row>
            <db:entry>Field Name</db:entry>
            <db:entry>Format</db:entry>
            <db:entry>Maximum Length</db:entry>
            <db:entry>Description</db:entry>
          </db:row>
        </db:thead>
        <db:tbody>
          <db:row>
            <db:entry>Item Code</db:entry>
            <db:entry>Character</db:entry>
            <db:entry>6</db:entry>
            <db:entry>Five or six alphanumeric characters in format (n)nnnna.</db:entry>
          </db:row>
          <db:row>
            <db:entry>Days</db:entry>
            <db:entry>Character</db:entry>
            <db:entry></db:entry>
            <db:entry>The number of days before another repeat can be filled.</db:entry>
          </db:row>
          <db:row>
            <db:entry>Counted</db:entry>
            <db:entry>Character</db:entry>
            <db:entry>1</db:entry>
            <db:entry>If 'Y', then if the script is filled within the specified number of days the script is counted against the Safety Net.  If 'N' then the script is not counted against the Safety Net.</db:entry>
          </db:row>
        </db:tbody>
      </db:tgroup>
    </db:table>
  </db:article>

  <xsl:output method="text"/>

  <xsl:variable name="delimiter" select="&quot;&#9;&quot;"/>

  <xsl:key name="ident" match="*" use="@xml:id"/>
  <xsl:key name="concept" match="skos:Concept" use="@rdf:about"/>

  <db:template name="root">
    <db:refpurpose>Check Document Version</db:refpurpose>
    <db:para>Make sure that the source document uses the correct version of the PBS XML
      Schema.</db:para>
  </db:template>
  <xsl:template match="/">
    <xsl:variable name="major" select="substring-before(/*/@version, &quot;.&quot;)"/>

    <xsl:choose>
      <xsl:when test="$major >= 2">
        <xsl:text>V99F</xsl:text>
        <xsl:value-of select="$delimiter"/>
        <xsl:value-of
          select="format-number(translate(key(&quot;concept&quot;, &quot;sn:default-days&quot;)/skos:note, &quot;PD&quot;, &quot;&quot;), &quot;00&quot;)"/>
        <xsl:value-of select="$delimiter"/>
        <xsl:value-of select="key(&quot;concept&quot;, &quot;sn:default-counted&quot;)/skos:note"/>
        <xsl:text>&#xa;</xsl:text>
        <xsl:text>0001A</xsl:text>
        <xsl:value-of select="$delimiter"/>
        <xsl:value-of
          select="format-number(translate(key(&quot;concept&quot;, &quot;sn:default-days&quot;)/skos:note, &quot;PD&quot;, &quot;&quot;), &quot;00&quot;)"/>
        <xsl:value-of select="$delimiter"/>
        <xsl:value-of select="key(&quot;concept&quot;, &quot;sn:default-counted&quot;)/skos:note"/>
        <xsl:text>&#xa;</xsl:text>
        <xsl:apply-templates
          select="/pbs:root/pbs:schedule/pbs:listings-list/pbs:prescribing-rule[not(pbs:drug-tariff)] |
          /pbs:root/pbs:schedule/pbs:listings-list/pbs:prescribing-rule/pbs:prescribing-rule">
          <xsl:sort select="substring(pbs:code, 1, string-length(pbs:code) - 1)" data-type="number"
          />
        </xsl:apply-templates>
      </xsl:when>
      <xsl:otherwise>
        <xsl:message terminate="yes">This stylesheet is designed to operate upon a Schedule XML
          document that conforms to version 2.0 (or later) of the PBS XML Schema. See
          http://xml.pbs.gov.au/ for more information on the PBS XML Schema, and for a version of
          this XSL stylesheet that operates on documents that conform to an earlier version of the
          schema.</xsl:message>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="pbs:prescribing-rule">

    <xsl:variable name="this" select="pbs:code"/>
    <!-- only process the first prescribing rule for the item -->
    <xsl:if test="not(preceding-sibling::pbs:prescribing-rule[1][pbs:code = $this])">

      <xsl:apply-templates select="pbs:code"/>
      <xsl:value-of select="$delimiter"/>
      <xsl:choose>
        <xsl:when test="pbs:solvent-rule">
          <xsl:variable name="injectable"
            select="key('ident', substring(*/pbs:injectable/pbs:listing-reference/@xlink:href, 2))"/>
          <!-- This assumes that the master injectable has SN data defined -->
          <xsl:value-of
            select="format-number(translate($injectable/*/pbs:safety-net/pbs:days, &quot;PD&quot;, &quot;&quot;), &quot;00&quot;)"/>
          <xsl:value-of select="$delimiter"/>
          <xsl:choose>
            <xsl:when test="$injectable/*/pbs:safety-net/pbs:counted">Y</xsl:when>
            <xsl:otherwise>N</xsl:otherwise>
          </xsl:choose>
        </xsl:when>
        <xsl:when test="*/pbs:safety-net">
          <xsl:value-of
            select="format-number(translate(*/pbs:safety-net/pbs:days, &quot;PD&quot;, &quot;&quot;), &quot;00&quot;)"/>
          <xsl:value-of select="$delimiter"/>
          <xsl:choose>
            <xsl:when test="*/pbs:safety-net/pbs:counted">Y</xsl:when>
            <xsl:otherwise>N</xsl:otherwise>
          </xsl:choose>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of
            select="format-number(translate(key(&quot;concept&quot;, &quot;sn:default-days&quot;)/skos:note, &quot;PD&quot;, &quot;&quot;), &quot;00&quot;)"/>
          <xsl:value-of select="$delimiter"/>
          <xsl:value-of select="key(&quot;concept&quot;, &quot;sn:default-counted&quot;)/skos:note"
          />
        </xsl:otherwise>
      </xsl:choose>
      <xsl:text>&#xa;</xsl:text>
    </xsl:if>
  </xsl:template>

  <!-- Codes are left-padded with zeroes -->
  <xsl:template match="pbs:code">
    <xsl:variable name="check" select="substring(., string-length(.))"/>
    <xsl:variable name="number" select="substring(., 1, string-length(.) - 1)"/>

    <xsl:value-of select="format-number($number, &quot;0000&quot;)"/>
    <xsl:value-of select="$check"/>
  </xsl:template>

  <xsl:template match="text()"/>
</xsl:stylesheet>
